<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title> - L_B__</title><meta name="referrer" content="no-referrer">
<meta name="description" content="利用string_view优化C&#43;&#43;的string"><meta property="og:title" content="" />
<meta property="og:description" content="利用string_view优化C&#43;&#43;的string" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" /><meta property="og:image" content="https://acking-you.github.io/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2022-03-24T00:00:00+00:00" />
<meta property="article:modified_time" content="2022-03-24T00:00:00+00:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://acking-you.github.io/logo.png"/>

<meta name="twitter:title" content=""/>
<meta name="twitter:description" content="利用string_view优化C&#43;&#43;的string"/>
<meta name="application-name" content="FeelIt">
<meta name="apple-mobile-web-app-title" content="FeelIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="canonical" href="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" /><link rel="prev" href="https://acking-you.github.io/posts/%E9%98%85%E8%AF%BBredis%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0%E8%B7%B3%E8%A1%A8/" /><link rel="next" href="https://acking-you.github.io/posts/1.3-%E9%94%81%E9%87%8A%E6%94%BE%E7%9A%84%E5%8F%A6%E4%B8%80%E7%A7%8D%E5%A7%BF%E5%8A%BF-%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8Fcondition_variable-%E4%BF%A1%E5%8F%B7%E9%87%8Fsemaphore/" /><link rel="stylesheet" href="/css/page.min.css"><link rel="stylesheet" href="/css/home.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/acking-you.github.io\/posts\/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c\u002b\u002b%E8%AF%AD%E6%B3%95\/"
        },"genre": "posts","keywords": "利用string_view优化string","wordcount":  3168 ,
        "url": "https:\/\/acking-you.github.io\/posts\/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c\u002b\u002b%E8%AF%AD%E6%B3%95\/","datePublished": "2022-03-24T00:00:00+00:00","dateModified": "2022-03-24T00:00:00+00:00","publisher": {
            "@type": "Organization",
            "name": "作者"},"author": {
                "@type": "Person",
                "name": "作者"
            },"description": "利用string_view优化C++的string"
    }
    </script></head><body data-header-desktop="auto" data-header-mobile="auto"><script>(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="L_B__">L_B__</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="#" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="L_B__">L_B__</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="#" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="#" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><div class="menu-item"><a href="javascript:void(0);" class="theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div></div>
    </div>
</header><div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single" data-toc="disable"><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/img_convert/1d3bc0dca5a88ba6649ae7b1041e8cf5.png#pic_center"
        data-srcset="https://img-blog.csdnimg.cn/img_convert/1d3bc0dca5a88ba6649ae7b1041e8cf5.png#pic_center, https://img-blog.csdnimg.cn/img_convert/1d3bc0dca5a88ba6649ae7b1041e8cf5.png#pic_center 1.5x, https://img-blog.csdnimg.cn/img_convert/1d3bc0dca5a88ba6649ae7b1041e8cf5.png#pic_center 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/img_convert/1d3bc0dca5a88ba6649ae7b1041e8cf5.png#pic_center"
        title="利用string_view优化C&#43;&#43;的string" /></div><div class="single-card" data-image="true"><h2 class="single-title animated flipInX"></h2><div class="post-meta">
                <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>作者</a></span>&nbsp;<span class="post-category">出版于  <a href="/categories/%E7%8E%B0%E4%BB%A3c++%E8%AF%AD%E6%B3%95/"><i class="far fa-folder fa-fw"></i>现代C++语法</a></span></div>
                <div class="post-meta-line"><span><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2022-03-24">2022-03-24</time></span>&nbsp;<span><i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 3168 字</span>&nbsp;
                    <span><i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 7 分钟</span>&nbsp;</div>
            </div>
            
            <hr><div class="details toc" id="toc-static"  data-kept="">
                    <div class="details-summary toc-title">
                        <span>目录</span>
                        <span><i class="details-icon fas fa-angle-right"></i></span>
                    </div>
                    <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#一背景">一、背景</a></li>
    <li><a href="#二stdstring_view">二、std::string_view</a>
      <ul>
        <li><a href="#21-构造函数">2.1 构造函数</a></li>
        <li><a href="#22-自定义字面量">2.2 自定义字面量</a></li>
        <li><a href="#23-成员函数">2.3 成员函数</a></li>
        <li><a href="#24-示例">2.4 示例</a></li>
      </ul>
    </li>
    <li><a href="#三使用陷阱">三、使用陷阱</a></li>
    <li><a href="#四测试具体性能">四、测试具体性能</a></li>
    <li><a href="#五总结">五、总结</a></li>
  </ul>
</nav></div>
                </div><div class="content" id="content"><h2 id="一背景">一、背景</h2>
<p>在日常C/C++编程中，我们常进行数据的传递操作，比如，将数据传给函数。当数据占用的内存较大时，减少数据的拷贝可以有效提高程序的性能。在<strong>C</strong>中指针是完成这一目的的标准数据结构，而<strong>C++引入了安全性更高的引用类型。所以在C++中若传递的数据仅仅只读，<code>const string&amp;</code>成了</strong>C++**的天然的方式。但这并非完美，从实践来看，它至少有以下几方面问题：</p>
<ol>
<li>字符串字面值、字符数组、字符串指针的传递仍要数据拷贝
这三类低级数据类型与<code>string</code>类型不同，传入时，编译器需要做隐式转换，即需要拷贝这些数据生成<code>string</code>临时对象。<code>const string&amp;</code>指向的实际上是这个临时对象。通常字符串字面值较小，性能损耗可以忽略不计；但字符串指针和字符数组某些情况下可能会比较大（比如读取文件的内容），此时会引起频繁的内存分配和数据拷贝，会严重影响程序的性能。</li>
<li><code>substr</code> <strong>O(n)</strong> 复杂度
这是一个特别常用的函数，好在<code>std::string</code>提供了这个函数，美中不足的是其每次都返回一个新生成的子串，很容易引起性能热点。实际上我们本意并不是要改变原字符串，为什么不在原字符串基础上返回呢？</li>
</ol>
<p>在<strong>C++17</strong>中引入了<code>string_view</code>，能很好的解决以上两个问题。</p>
<h2 id="二stdstring_view">二、std::string_view</h2>
<p>从名字出发，我们可以类比数据库视图，<code>view</code>表示该类型不会为数据分配存储空间，而且该数据类型只能用来读。该数据类型可通过<code>{数据的起始指针，数据的长度}</code>两个元素表示，实际上该数据类型的实例不会具体存储原数据，仅仅存储指向的数据的起始指针和长度，所以这个开销是非常小的。</p>
<p>要使用字符串视图，需要引入<code>&lt;string_view&gt;</code>，下面介绍该数据类型主要的API。这些API基本上都有<code>constexpr</code>修饰，所以能在编译时很好地处理字符串字面值，从而提高程序效率。</p>
<h3 id="21-构造函数">2.1 构造函数</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="k">constexpr</span> <span class="nf">string_view</span><span class="p">()</span> <span class="k">noexcept</span><span class="p">;</span>
<span class="k">constexpr</span> <span class="nf">string_view</span><span class="p">(</span><span class="k">const</span> <span class="n">string_view</span><span class="o">&amp;</span> <span class="n">other</span><span class="p">)</span> <span class="k">noexcept</span> <span class="o">=</span> <span class="k">default</span><span class="p">;</span>
<span class="k">constexpr</span> <span class="nf">string_view</span><span class="p">(</span><span class="k">const</span> <span class="n">CharT</span><span class="o">*</span> <span class="n">s</span><span class="p">,</span> <span class="n">size_type</span> <span class="n">count</span><span class="p">);</span>
<span class="k">constexpr</span> <span class="nf">string_view</span><span class="p">(</span><span class="k">const</span> <span class="n">CharT</span><span class="o">*</span> <span class="n">s</span><span class="p">);</span>
</code></pre></div><p>唯一需要说明的是：为什么我们代码<code>string_view foo(string(&quot;abc&quot;))</code>可以编译通过，但为什么没有对应的构造函数？</p>
<p>实际上这是因为<code>string</code>类重载了<code>string</code>到<code>string_view</code>的转换操作符：
<code>operator std::basic_string_view&lt;CharT, Traits&gt;() const noexcept;</code></p>
<h3 id="22-自定义字面量">2.2 自定义字面量</h3>
<p>自定义字面量也是C++17新增的特性，提高了常量的易读。
下面的代码取值<strong>cppreference</strong>，能很好地说明自定义字面值和字符串语义的差异。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;string_view&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp"></span> 
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="o">::</span><span class="n">literals</span><span class="p">;</span>
 
    <span class="n">std</span><span class="o">::</span><span class="n">string_view</span> <span class="n">s1</span> <span class="o">=</span> <span class="s">&#34;abc</span><span class="se">\0\0</span><span class="s">def&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">string_view</span> <span class="n">s2</span> <span class="o">=</span> <span class="s">&#34;abc</span><span class="se">\0\0</span><span class="s">def&#34;</span><span class="n">sv</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;s1: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">s1</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&#34; </span><span class="se">\&#34;</span><span class="s">&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">s1</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;</span><span class="se">\&#34;\n</span><span class="s">&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;s2: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">s2</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&#34; </span><span class="se">\&#34;</span><span class="s">&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">s2</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;</span><span class="se">\&#34;\n</span><span class="s">&#34;</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>输出：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-apache" data-lang="apache"><span class="err">s1:</span> <span class="err">3</span> <span class="err">&#34;abc&#34;</span>
<span class="err">s2:</span> <span class="err">8</span> <span class="err">&#34;abc^@^@def&#34;</span>
</code></pre></div><p>以上例子能很好看清二者的语义区别，<code>\0</code>对于字符串而言，有其特殊的意义，即表示字符串的结束，字符串视图根本不care，它关心实际的字符个数。</p>
<h3 id="23-成员函数">2.3 成员函数</h3>
<p>下面列举其成员函数：忽略了函数的返回值，若函数有重载，括号内用<code>...</code>填充。这样可以对其有个整体轮廓。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="c1">// 迭代器
</span><span class="c1"></span><span class="n">begin</span><span class="p">()</span>
<span class="n">end</span><span class="p">()</span>
<span class="n">cbegin</span><span class="p">()</span>
<span class="n">cend</span><span class="p">()</span>
<span class="n">rbegin</span><span class="p">()</span>
<span class="n">rend</span><span class="p">()</span>
<span class="n">crbegin</span><span class="p">()</span>
<span class="n">crend</span><span class="p">()</span>
 
<span class="c1">// 容量
</span><span class="c1"></span><span class="n">size</span><span class="p">()</span>
<span class="n">length</span><span class="p">()</span>
<span class="n">max_size</span><span class="p">()</span>
<span class="n">empty</span><span class="p">()</span>
 
<span class="c1">// 元素访问
</span><span class="c1"></span><span class="k">operator</span><span class="p">[](</span><span class="n">size_type</span> <span class="n">pos</span><span class="p">)</span>
<span class="n">at</span><span class="p">(</span><span class="n">size_type</span> <span class="n">pos</span><span class="p">)</span>
<span class="n">front</span><span class="p">()</span>
<span class="n">back</span><span class="p">()</span>
<span class="n">data</span><span class="p">()</span>
 
<span class="c1">// 修改器
</span><span class="c1"></span><span class="n">remove_prefix</span><span class="p">(</span><span class="n">size_type</span> <span class="n">n</span><span class="p">)</span>
<span class="n">remove_suffix</span><span class="p">(</span><span class="n">size_type</span> <span class="n">n</span><span class="p">)</span>
<span class="n">swap</span><span class="p">(</span><span class="n">basic_string_view</span><span class="o">&amp;</span> <span class="n">s</span><span class="p">)</span>
 
<span class="n">copy</span><span class="p">(</span><span class="n">charT</span><span class="o">*</span> <span class="n">s</span><span class="p">,</span> <span class="n">size_type</span> <span class="n">n</span><span class="p">,</span> <span class="n">size_type</span> <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">string_view</span> <span class="n">substr</span><span class="p">(</span><span class="n">size_type</span> <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">size_type</span> <span class="n">n</span> <span class="o">=</span> <span class="n">npos</span><span class="p">)</span>
<span class="n">compare</span><span class="p">(...)</span>
<span class="n">starts_with</span><span class="p">(...)</span>
<span class="n">ends_with</span><span class="p">(...)</span>
<span class="n">find</span><span class="p">(...)</span>
<span class="n">rfind</span><span class="p">(...)</span>
<span class="n">find_first_of</span><span class="p">(...)</span>
<span class="n">find_last_of</span><span class="p">(...)</span> 
<span class="n">find_first_not_of</span><span class="p">(...)</span>
<span class="n">find_last_not_of</span><span class="p">(...)</span>
</code></pre></div><p>从函数列表来看，几乎跟<code>string</code>的只读函数一致，使用<code>string_view</code>的方式跟<code>string</code>基本一致。有几个地方需要特别说明：</p>
<ol>
<li><code>string_view</code>的<code>substr</code>函数的时间复杂度是O(1)，解决了<strong>背景</strong>部分的第二个问题。</li>
<li><strong>修改器</strong>中的三个函数仅会修改<code>string_view</code>的数据指向，不会修改指向的数据。</li>
</ol>
<p>除此之外，函数名基本是自解释的。</p>
<h3 id="24-示例">2.4 示例</h3>
<p><strong>Haskell</strong> 中有一个常用函数<code>lines</code>，会将字符串切割成行存储在容器里。下面我们用 <strong>C++</strong> 来实现</p>
<p><strong>string-版本</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;algorithm&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;sstream&gt;</span><span class="cp">
</span><span class="cp"></span>
<span class="kt">void</span> <span class="nf">lines</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">lines</span><span class="p">,</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="n">str</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">auto</span> <span class="n">sep</span><span class="p">{</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">};</span>
    <span class="n">size_t</span> <span class="n">start</span><span class="p">{</span><span class="n">str</span><span class="p">.</span><span class="n">find_first_not_of</span><span class="p">(</span><span class="n">sep</span><span class="p">)};</span>
    <span class="n">size_t</span> <span class="n">end</span><span class="p">{};</span>

    <span class="k">while</span> <span class="p">(</span><span class="n">start</span> <span class="o">!=</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">::</span><span class="n">npos</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">end</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">find_first_of</span><span class="p">(</span><span class="n">sep</span><span class="p">,</span> <span class="n">start</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">end</span> <span class="o">==</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">::</span><span class="n">npos</span><span class="p">)</span>
            <span class="n">end</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">length</span><span class="p">();</span>

        <span class="n">lines</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">str</span><span class="p">.</span><span class="n">substr</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">-</span> <span class="n">start</span><span class="p">));</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">find_first_not_of</span><span class="p">(</span><span class="n">sep</span><span class="p">,</span> <span class="n">end</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><p>上面我们用<code>const std::string &amp;</code>类型接收待分割的字符串，若我们传入指向较大内存的字符指针时，会影响程序效率。</p>
<p>使用<code>std::string_view</code>可以避免这种情况：
<strong>string_view-版本</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;algorithm&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;sstream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;string_view&gt;</span><span class="cp">
</span><span class="cp"></span>
<span class="kt">void</span> <span class="nf">lines</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">lines</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">string_view</span> <span class="n">str</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">auto</span> <span class="n">sep</span><span class="p">{</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">};</span>
    <span class="n">size_t</span> <span class="n">start</span><span class="p">{</span><span class="n">str</span><span class="p">.</span><span class="n">find_first_not_of</span><span class="p">(</span><span class="n">sep</span><span class="p">)};</span>
    <span class="n">size_t</span> <span class="n">end</span><span class="p">{};</span>

    <span class="k">while</span> <span class="p">(</span><span class="n">start</span> <span class="o">!=</span> <span class="n">std</span><span class="o">::</span><span class="n">string_view</span><span class="o">::</span><span class="n">npos</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">end</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">find_first_of</span><span class="p">(</span><span class="n">sep</span><span class="p">,</span> <span class="n">start</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">end</span> <span class="o">==</span> <span class="n">std</span><span class="o">::</span><span class="n">string_view</span><span class="o">::</span><span class="n">npos</span><span class="p">)</span>
            <span class="n">end</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">length</span><span class="p">();</span>

        <span class="n">lines</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">{</span><span class="n">str</span><span class="p">.</span><span class="n">substr</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">-</span> <span class="n">start</span><span class="p">)});</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">find_first_not_of</span><span class="p">(</span><span class="n">sep</span><span class="p">,</span> <span class="n">end</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><p>上面的例子仅仅是把<code>string</code>类型修改成了<code>string_view</code>就获得了性能上的提升。一般情况下，将程序中的<code>string</code>换成<code>string_view</code>的过程是比较直观的，这得益于两者的成员函数的相似性。但并不是所有的“翻译”过程都是这样的，比如：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="kt">void</span> <span class="nf">lines</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">lines</span><span class="p">,</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">str</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">stringstream</span> <span class="n">ss</span><span class="p">(</span><span class="n">str</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">line</span><span class="p">;</span>

    <span class="k">while</span> <span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">getline</span><span class="p">(</span><span class="n">ss</span><span class="p">,</span> <span class="n">line</span><span class="p">,</span> <span class="sc">&#39;\n&#39;</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">lines</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">line</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><p>这个版本使用<code>stringstream</code>实现<code>lines</code>函数。由于<code>stringstream</code>没有相应的构造函数接收<code>string_view</code>类型参数，所以没法采用直接替换的方式，所以翻译过程要复杂点。</p>
<h2 id="三使用陷阱">三、使用陷阱</h2>
<p>世上没有免费的午餐。不恰当的使用<code>string_view</code>也会带来一系列的问题。</p>
<ol>
<li>
<p><code>string_view</code>范围内的字符可能不包含<code>\0</code>
如：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;string_view&gt;</span><span class="cp">
</span><span class="cp"></span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">string_view</span> <span class="n">str</span><span class="p">{</span><span class="s">&#34;abc&#34;</span><span class="p">,</span> <span class="mi">1</span><span class="p">};</span>

    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">str</span><span class="p">.</span><span class="n">data</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>本来是要打印<code>a</code>，但输出了<code>abc</code>。这是因为字符串相关的函数都有一条兼容C的约定：<code>\0</code>代表字符串的结尾。上面的程序打印从开始到字符串结束的所有字符，虽然<code>str</code>包含的有效字符是<code>a</code>，但<code>cout</code>认<code>\0</code>。好在这块内存空间有合法的字符串结尾符，如果<code>str</code>指向的是一个没有<code>\0</code>的字符数组，程序很有可能会出现内存问题，所以我们在将<code>string_view</code>类型的数据传入接收字符串的函数时要非常小心。</p>
</li>
<li>
<p>从<code>[const] char*</code>构造<code>string_view</code>对象时间复杂度<code>O(n)</code>
这是因为获取字符串的长度需要从头开始遍历。如果对<code>[const] char*</code>类型仅仅是一些<code>O(1)</code>的操作，相比直接使用<code>[const] char*</code>，转为<code>string_view</code>是没有性能优势的。只不过是相比<code>const string&amp;</code>，<code>string_view</code>少了拷贝的损耗。实际上我们完全可以用<code>[const] char*</code>接收所有的字符串，但这个类型太底层了，不便使用。在某些情况下，我们转为<code>string_view</code>可能仅仅是想用其中的一些函数，比如<code>substr</code>。</p>
</li>
<li>
<p><code>string_view</code>指向的内容的生命周期可能比其本身短
<code>string_view</code>并不拥有其指向内容的所有权，用Rust的术语来说，它仅仅是暂时<code>borrow</code>（借用）了它。如果拥有者提前释放了，你还在使用这些内容，那会出现内存问题，这跟<code>悬挂指针</code>(dangling pointer)或悬挂引用（dangling references）很像。Rust专门有套机制在编译时分析变量的生命期，保证<code>borrow</code>的资源在使用期间不会被释放，但C++没有这样的检查，需要人工保证。下面列出一些典型的问题情况：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="n">std</span><span class="o">::</span><span class="n">string_view</span> <span class="n">sv</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">{</span><span class="s">&#34;hello world&#34;</span><span class="p">};</span> 
<span class="n">string_view</span> <span class="nf">foo</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">s</span><span class="p">{</span><span class="s">&#34;hello world&#34;</span><span class="p">};</span>
    <span class="k">return</span> <span class="n">string_view</span><span class="p">{</span><span class="n">s</span><span class="p">};</span>
<span class="p">}</span>
<span class="k">auto</span> <span class="nf">id</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">string_view</span> <span class="n">sv</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">sv</span><span class="p">;</span> <span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">s</span> <span class="o">=</span> <span class="s">&#34;hello&#34;</span><span class="p">;</span>
    <span class="k">auto</span> <span class="n">sv</span> <span class="o">=</span> <span class="n">id</span><span class="p">(</span><span class="n">s</span> <span class="o">+</span> <span class="s">&#34; world&#34;</span><span class="p">);</span> 
<span class="p">}</span>
</code></pre></div></li>
</ol>
<h2 id="四测试具体性能">四、测试具体性能</h2>
<p>光说不用假把式，直接就地写个测试：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;string_view&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&#34;../BenchMark/Timer.h&#34;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp">
</span><span class="cp"></span><span class="k">using</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="p">;</span>
<span class="k">using</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">;</span>
<span class="k">using</span> <span class="n">std</span><span class="o">::</span><span class="n">string_view</span><span class="p">;</span>
<span class="c1">//TODO string_view 和 string const&amp;对比。
</span><span class="c1">// 发现string_view在substr()方法和vector的push_back过程中有巨大优势！
</span><span class="c1"></span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;</span> <span class="n">testStringView</span><span class="p">(</span><span class="n">string_view</span> <span class="n">s</span><span class="p">){</span>
    <span class="n">s</span><span class="p">.</span><span class="n">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
    <span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span> <span class="o">&gt;</span><span class="n">ret</span><span class="p">;</span>
    <span class="n">ret</span><span class="p">.</span><span class="n">emplace_back</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">ret</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span><span class="o">&gt;</span> <span class="n">testString</span><span class="p">(</span><span class="n">string</span> <span class="k">const</span><span class="o">&amp;</span><span class="n">s</span><span class="p">){</span>
    <span class="n">s</span><span class="p">.</span><span class="n">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
    <span class="n">vector</span><span class="o">&lt;</span><span class="n">string</span> <span class="o">&gt;</span><span class="n">ret</span><span class="p">;</span>
    <span class="n">ret</span><span class="p">.</span><span class="n">emplace_back</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">ret</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">//TEST string const&amp;
</span><span class="c1"></span>    <span class="p">{</span>
        <span class="n">Timer</span> <span class="n">t</span><span class="p">;</span>
        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">1000000</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">testString</span><span class="p">(</span><span class="n">string</span> <span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="sc">&#39;0&#39;</span><span class="p">));</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="c1">//TEST string_view
</span><span class="c1"></span>    <span class="p">{</span>
        <span class="n">Timer</span> <span class="n">t</span><span class="p">;</span>
        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">1000000</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">testStringView</span><span class="p">(</span><span class="n">string</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="sc">&#39;0&#39;</span><span class="p">));</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>性能差距：</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/7c76f25f36564760b6d68fb417d71a26.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQysrKysrKysrKysrKysrKysrKys=,size_20,color_FFFFFF,t_70,g_se,x_16"
        data-srcset="https://img-blog.csdnimg.cn/7c76f25f36564760b6d68fb417d71a26.png?x-oss-process=image/watermark%2ctype_d3F5LXplbmhlaQ%2cshadow_50%2ctext_Q1NETiBAQysrKysrKysrKysrKysrKysrKys=%2csize_20%2ccolor_FFFFFF%2ct_70%2cg_se%2cx_16, https://img-blog.csdnimg.cn/7c76f25f36564760b6d68fb417d71a26.png?x-oss-process=image/watermark%2ctype_d3F5LXplbmhlaQ%2cshadow_50%2ctext_Q1NETiBAQysrKysrKysrKysrKysrKysrKys=%2csize_20%2ccolor_FFFFFF%2ct_70%2cg_se%2cx_16 1.5x, https://img-blog.csdnimg.cn/7c76f25f36564760b6d68fb417d71a26.png?x-oss-process=image/watermark%2ctype_d3F5LXplbmhlaQ%2cshadow_50%2ctext_Q1NETiBAQysrKysrKysrKysrKysrKysrKys=%2csize_20%2ccolor_FFFFFF%2ct_70%2cg_se%2cx_16 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/7c76f25f36564760b6d68fb417d71a26.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQysrKysrKysrKysrKysrKysrKys=,size_20,color_FFFFFF,t_70,g_se,x_16"
        title="https://img-blog.csdnimg.cn/7c76f25f36564760b6d68fb417d71a26.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQysrKysrKysrKysrKysrKysrKys=,size_20,color_FFFFFF,t_70,g_se,x_16" /></p>
<p>除了以上代码，我还经过多次测试，发现在用到substr成员函数以及进行各种拷贝的时候，string_view会比string const&amp;快很多。其余情况差不太多。</p>
<h2 id="五总结">五、总结</h2>
<p><code>string_view</code>解决了一些痛点，但同时也引入了指针和引用的一些老问题。C++标准并没有对这个类型做太多的约束，这引来的问题是我们可以像平常的变量一样以多种方式使用它，如，可以传参，可以作为函数返回值，可以做普通变量，甚至我们可以放到容器里。随着使用场景的复杂，人工是很难保证指向的内容的生命周期足够长。所以，推荐的使用方式：<strong>仅仅作为函数参数</strong>，因为如果该参数仅仅在函数体内使用而不传递出去，这样使用是安全的。</p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info"><div class="post-info-tag"><span><a href="/tags/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96string/">利用string_view优化string</a>
                </span></div><div class="post-info-line"><div class="post-info-mod">
                <span>更新于 2022-03-24</span>
            </div><div class="post-info-mod"></div>
        </div><div class="post-info-share">
            <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" data-title="" data-hashtags="利用string_view优化string"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" data-hashtag="利用string_view优化string"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 WhatsApp" data-sharer="whatsapp" data-url="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" data-title="" data-web><i class="fab fa-whatsapp fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" data-title=""><i class="fab fa-line fa-fw"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" data-title="" data-image="https://img-blog.csdnimg.cn/img_convert/1d3bc0dca5a88ba6649ae7b1041e8cf5.png#pic_center"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" data-title="" data-description="利用string_view优化C&#43;&#43;的string"><i data-svg-src="/lib/simple-icons/icons/myspace.min.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" data-title="" data-description="利用string_view优化C&#43;&#43;的string"><i class="fab fa-blogger fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Evernote" data-sharer="evernote" data-url="https://acking-you.github.io/posts/%E5%88%A9%E7%94%A8string_view%E4%BC%98%E5%8C%96%E7%8E%B0%E4%BB%A3c&#43;&#43;%E8%AF%AD%E6%B3%95/" data-title=""><i class="fab fa-evernote fa-fw"></i></a></span>
        </div></div><div class="post-nav"><a href="/posts/%E9%98%85%E8%AF%BBredis%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0%E8%B7%B3%E8%A1%A8/" class="prev" rel="prev" title="通过阅读Redis源码简单实现跳表"><i class="fas fa-angle-left fa-fw"></i>Previous Post</a>
            <a href="/posts/1.3-%E9%94%81%E9%87%8A%E6%94%BE%E7%9A%84%E5%8F%A6%E4%B8%80%E7%A7%8D%E5%A7%BF%E5%8A%BF-%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8Fcondition_variable-%E4%BF%A1%E5%8F%B7%E9%87%8Fsemaphore/" class="next" rel="next" title="1.3-多线程控制的另一种姿势-条件变量(condition_variable), 信号量(semaphore)">Next Post<i class="fas fa-angle-right fa-fw"></i></a></div></div>
</div></article></div>
            </main>
            <footer class="footer"><div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.86.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/khusika/FeelIt" target="_blank" rel="noopener noreffer" title="FeelIt 1.0.1"><i class="fas fa-hand-holding-heart fa-fw"></i> FeelIt</a>
        </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2023</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/"></a></span></div>
</div>
</footer>
        </div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-chevron-up fa-fw"></i>
            </a></div><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><link rel="stylesheet" href="/lib/katex/katex.min.css"><link rel="stylesheet" href="/lib/katex/copy-tex.min.css"><script src="/lib/autocomplete/autocomplete.min.js"></script><script src="/lib/lunr/lunr.min.js"></script><script src="/lib/lunr/lunr.stemmer.support.min.js"></script><script src="/lib/lunr/lunr.zh.min.js"></script><script src="/lib/lazysizes/lazysizes.min.js"></script><script src="/lib/clipboard/clipboard.min.js"></script><script src="/lib/sharer/sharer.min.js"></script><script src="/lib/katex/katex.min.js"></script><script src="/lib/katex/auto-render.min.js"></script><script src="/lib/katex/copy-tex.min.js"></script><script src="/lib/katex/mhchem.min.js"></script><script>window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":200},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":100,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script src="/js/theme.min.js"></script></body></html>
